package com.aggron.concurrency.examples.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author LR2SD
 * @description Synchronized修饰代码块和方法
 * @date 2018-06-10 10:51
 */
@Slf4j
public class SynchronizedExample1 {
    public void test1(int k){
        synchronized (this){
            for (int i = 0; i < 10; i++) {
                log.info("test1-{}, {}",k,i);
            }
        }

    }
    public synchronized void test2(int k){
        for (int i = 0; i < 10; i++) {
            log.info("test2-{}, {}",k,i);
        }
    }
    public static void main(String [] args){
        SynchronizedExample1 example1=new SynchronizedExample1();
        SynchronizedExample1 example2=new SynchronizedExample1();
        ExecutorService executorService= Executors.newCachedThreadPool();
        executorService.execute(()->{
            example1.test2(1);
        });
        executorService.execute(()->{
            example2.test2(2);
        });
        executorService.shutdown();
    }
}
